<?php
/**
 * 获取
 *
 *
 */

use Workerman\Worker;
use Workerman\Mysql;

class get_poster_list{

    public function entry($param){

        return $this->do_get_poster_list($param);
    }


    /**
     * do_get_poster_list
     *
     *
     * @access private
     * @param $param
     * @since 1.0
     * @return array(errcode,errmsg,rtv)
     */
    private function do_get_poster_list($param){
        // hf_mes_poster_management
        $now_date = date("Y-m-d H:i:s");
        //var_dump($now_date);
        $history = @$param["history"];
        $user_id = $param["user_id"];
        $department_headquarters_id = 0;
        // 是否为总监
        $is_chief_inspector = 0;
        if($history == 1){
            // 查看历史数据
            $sql = "select b.id,a.name,b.number,a.start_time,a.end_time,count(c.id) as amount FROM hf_mes_poster_management a 
					INNER JOIN hf_mes_poster_contents b ON a.id=b.poster_id 
					INNER JOIN hf_mes_poster_picture c ON b.id=c.poster_contents_id 
					INNER JOIN hf_mes_poster_picture_department d ON c.id =d.picture_id 
					INNER JOIN  hf_mes_user e ON d.department_id=e.department 
					WHERE e.id='$user_id' and d.finish=1 GROUP BY b.id";
        }else{
            $sql = "select c.department_headquarters_id,b.class,a.department from hf_mes_user a 
					inner join hf_mes_groups b ON a.groups=b.id 
					inner join hf_mes_department c ON a.department=c.id
					where a.id='$user_id'";
            $ret_user = sql_query($sql);

            // group为1、2、3,分别代表采购员、副总监、总监
            $department_headquarters_id = $ret_user[0]["department_headquarters_id"]; //总部门ID
            $department_id = $ret_user[0]["department"]; //分部门ID

            $sql_str = "d.department_id='$department_id' and d.finish=0 ";
            if($ret_user[0]["class"] == "副总监"){
                $sql_str = "e.id='$user_id' and d.finish=1 and d.deputy_director='-1'";
            }elseif($ret_user[0]["class"] == "总监"){
                // 如果是总监，先判断当次校稿当前部门下所有副总监都已经校稿完成
                $sql_str = "d.chief_inspector='-1' and d.deputy_director!='-1' AND d.department_id IN (SELECT id FROM hf_mes_department where department_headquarters_id='$department_headquarters_id')";

                $is_chief_inspector = 1;
            }
            $sql = "select b.id,a.name,b.number,a.start_time,a.end_time,d.finish,c.id as cid FROM hf_mes_poster_management a
					INNER JOIN hf_mes_poster_contents b ON a.id=b.poster_id
					INNER JOIN hf_mes_poster_picture c ON b.id=c.poster_contents_id
					INNER JOIN hf_mes_poster_picture_department d ON c.id =d.picture_id 
					INNER JOIN  hf_mes_user e ON d.department_id=e.department 
					WHERE $sql_str and a.end_time>'$now_date' GROUP BY b.id";

        }
        $dp=sql_query($sql);

        //  如果是总监，先判断当次校稿当前部门下所有副总监都已经校稿完成
        if($is_chief_inspector == 1){
            $ret_val = [];
            foreach($dp as $key=>$val){

                $poster_contents_id = $val["id"];

                $sql = "select d.id from hf_mes_poster_contents b
						INNER JOIN hf_mes_poster_picture c ON b.id=c.poster_contents_id
						INNER JOIN hf_mes_poster_picture_department d ON c.id =d.picture_id 

						WHERE  b.id='$poster_contents_id' and d.deputy_director!='-1' AND d.department_id IN (SELECT id FROM hf_mes_department where department_headquarters_id='$department_headquarters_id')";


                $ret1=sql_query($sql);

                if(count($ret1) >= 0){
                    foreach($val as $k=>$v){

                        if($k == "id"){
                            $sql = "SELECT count(page) as page_amount FROM hf_mes_poster_picture WHERE poster_contents_id='$v' GROUP BY page";
                            $ret = sql_query($sql);

                            $dp[$key]["page_amount"] = $ret[0]["page_amount"];
                            // 重新计算海报数量，需要计算按部门筛选
                            $sql = "SELECT b.id FROM hf_mes_poster_picture a 
									INNER JOIN hf_mes_poster_picture_department b ON a.id =b.picture_id 
									INNER JOIN hf_mes_user c ON b.department_id=c.department 
									INNER JOIN hf_mes_department d ON c.department=d.id 
									WHERE a.poster_contents_id='$v' AND d.department_headquarters_id='$department_headquarters_id' GROUP BY b.id";
                            $ret = sql_query($sql);

                            $dp[$key]["amount"] = count($ret);
                        }
                    }
                    array_push($ret_val,$dp[$key]);
                }

            }
            $dp = $ret_val;

        }else{
            // 查看当期海报页数量
            foreach($dp as $key=>$val){

                $poster_contents_id = $val["id"];

                $sql = "SELECT count(a.id) as page_amount
                FROM hf_mes_poster_picture as a 
                INNER JOIN hf_mes_poster_picture_department as b ON a.id=b.picture_id
                WHERE a.poster_contents_id='$poster_contents_id'
                AND b.department_id='$department_id'
                GROUP BY a.poster_contents_id";

                $ret = sql_query($sql);

                $dp[$key]["page_amount"] = $ret[0]["page_amount"];

                $dp[$key]["amount"] = $ret[0]["page_amount"];


            }
        }


        return array("errcode"=>0, "errmsg"=>"", "rtval"=>$dp);

    }


    /**
     * create
     * 通过 call_user_func 获得本class的实例
     *
     * @access public
     * @since 1.0
     * @return object 本类的实例
     */
    public static function create(){
        static $instance;
        if (!$instance){
            $instance = new get_poster_list();
        }
        return $instance;
    }
}


?>